package lib

import (
	"fmt"

	"github.com/gokyle/sshkey"
)

//KeyGen ...
func KeyGen(keytype, comment string, size int) (string, string, error) {
	var (
		err error
		key interface{}
	)

	switch keytype {
	case "rsa":
		fmt.Println("=========>Generating public/private rsa key pair.")
		key, err = sshkey.GenerateKey(sshkey.KEY_RSA, size)
		if err != nil {
			HandleError(err)
			return "", "", err
		}
	case "ecdsa":
		fmt.Println("=========>Generating public/private ecdsa key pair.")
		key, err = sshkey.GenerateKey(sshkey.KEY_ECDSA, size)
		if err != nil {
			HandleError(err)
			return "", "", err
		}
	}

	privout, err := sshkey.MarshalPrivate(key, "")
	if err != nil {
		HandleError(err)
		return "", "", err
	}
	pub := sshkey.NewPublic(key, comment)
	if key == nil {
		fmt.Println("Failed to create a public key.")
		return "", "", err
	}
	pubout := sshkey.MarshalPublic(pub)
	if pubout == nil {
		fmt.Println("Failed to create a public key.")
		return "", "", err
	}
	fmt.Println(string(privout), string(pubout))
	return string(privout), string(pubout), nil
}
